package com.example.administrator.shihuo.activities;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.example.administrator.shihuo.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import de.greenrobot.event.EventBus;

public class LoginActivity extends AppCompatActivity {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initData();
    }

    private void initData() {
        sharedPreferences = getSharedPreferences("isLogin", MODE_PRIVATE);
        edit = sharedPreferences.edit();
    }

    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()) {
                case R.id.iv_loginactivity_qq:
                    //第三方登录；
                    Platform qq = ShareSDK.getPlatform(QQ.NAME);
                    qq.setPlatformActionListener(new PlatformActionListener() {
                        @Override
                        public void onComplete(Platform platform, int i, final HashMap<String, Object> res) {
                            //用户资源都保存到res
//通过打印res数据看看有哪些数据是你想要
                            PlatformDb platDB = platform.getDb();//获取数平台数据DB
                            //通过DB获取各种数据
                            platDB.getToken();
                            platDB.getUserGender();
                            String userIcon = platDB.getUserIcon();
                            platDB.getUserId();
                            String userName = platDB.getUserName();
                            Bundle bundle = new Bundle();
                            bundle.putString("userIcon", userIcon);
                            bundle.putString("userName", userName);
                            EventBus.getDefault().post(bundle);
                            EventBus.getDefault().post(true);
                            //写入sharepreferences；
                            edit.putString("userIcon", userIcon);
                            edit.putString("userName", userName);
                            edit.putBoolean("isLogin", true);
                            edit.commit();
                        }

                        @Override
                        public void onError(Platform platform, int i, Throwable throwable) {
                        }

                        @Override
                        public void onCancel(Platform platform, int i) {
                        }
                    });
                    qq.authorize();
//移除授权
//weibo.removeAccount(true);
                    finish();
                    break;
                case R.id.iv_loginactivity_back:
                    finish();
                    break;
            }
        }
    }
}
